package com.wkq.net.callback

import com.wkq.net.model.BaseModel
import io.reactivex.rxjava3.core.Observer
import io.reactivex.rxjava3.disposables.Disposable
import retrofit2.HttpException

/**
 *
 *@Author: wkq
 *
 *@Time: 2025/4/9 14:50
 *
 *@Desc:
 */
class DataObserver<T >(var callback:DataCallback<T>): Observer<BaseModel<T>> {
    override fun onSubscribe(d: Disposable) {

    }

    override fun onError(t: Throwable) {

        if (t is HttpException) {
            val ex: HttpException = t
            callback.onFail(ex.code(),ex.message)

        } else {
            callback.onFail(-1,t.message?:"")
        }
    }

    override fun onComplete() {

    }

    override fun onNext(baseModel: BaseModel<T>) {
        if (200 == baseModel.api_code) {
            callback.success(baseModel.data)
        } else {
            callback.onFail(baseModel.api_code, baseModel.api_msg)
        }

    }
}